import request from '@/config/axios'
import { getRefreshToken } from '@/utils/auth'
import type { UserLoginVO } from './types'

export interface SmsCodeVO {
  mobile: string
  scene: number
}

export interface SmsLoginVO {
  mobile: string
  code: string
}

export interface AppAuthSmsReqVO {
  accountId: number
  captchaVerification: string
}

export interface AppAuthSmsVerifyVO {
  accountId: number
  code: string
}

export interface AppAuthSmsInfoVO {
  accountId: number
  token: string
}

export interface AppAuthActiveReqVO {
  accountId: number
  password: string
  token: string
}

/**
 * 重置账号密码短信请求 VO
 */
export interface AuthResetPasswordReqVO {
  username: string // 登录账号
  captchaVerification: string // 滑动验证码
}

/**
 * 重置账号密码短信请求 VO
 */
export interface AuthResetPasswordSaveReqVO {
  username: string // 登录账号
  password: string // 新密码
  code: string // 短信验证码
}

// C端科大统一登录
export const ustcLogin = (data: UserLoginVO) => {
  return request.post({ url: '/system/auth/ustc-login', data })
}

// USTC获取授权码模式跳转地址
export const getAuthorizeUrl = (redirectUri: string) => {
  return request.get({ url: '/system/auth/ustc-authorize-url?redirectUri=' + redirectUri })
}

// C端USTC授权码模式登录
export const ustcAuthorizeLogin = (code: string,redirectUri: string) => {
  return request.get({ url: '/system/auth/ustc-authorize-login?code=' + code + '&redirectUri=' + redirectUri })
}

// C端账号密码登录
export const login = (data: UserLoginVO) => {
  return request.post({ url: '/system/auth/login', data })
}

// 登出
export const loginOut = () => {
  return request.post({ url: '/system/auth/logout' })
}

// 获取用户权限信息
export const getInfo = () => {
  return request.get({ url: '/core/auth/get-info' })
}

//获取登录验证码
export const sendSmsCode = (data: SmsCodeVO) => {
  return request.post({ url: '/system/auth/send-sms-code', data })
}

// 短信验证码登录
export const smsLogin = (data: SmsLoginVO) => {
  return request.post({ url: '/system/auth/sms-login', data })
}

// 社交快捷登录，使用 code 授权码
export function socialLogin(type: string, code: string, state: string) {
  return request.post({
    url: '/system/auth/social-login',
    data: {
      type,
      code,
      state
    }
  })
}

// 社交授权的跳转
export const socialAuthRedirect = (type: number, redirectUri: string) => {
  return request.get({
    url: '/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri
  })
}
// 获取验证图片以及 token
export const getCode = (data) => {
  return request.postOriginal({ url: '/system/captcha/get', data })
}

// 滑动或者点选验证
export const reqCheck = (data) => {
  return request.postOriginal({ url: '/system/captcha/check', data })
}

// C端激活第一步-通过学号查找信息
export const getAccountActiveInfo = (username: string, accountType: number) => {
  return request.get({ url: `/system/auth/info?username=${username}&accountType=${accountType}` })
}

// C端激活第二步-向指定账号的手机发送验证码短信
export const sendSms = (data: AppAuthSmsReqVO) => {
  return request.post({ url: '/system/auth/send-sms', data })
}

// C端激活第三步-验证发给指定账号的手机的验证码
export const verifySms = (data: AppAuthSmsVerifyVO) => {
  return request.post({ url: '/system/auth/verify-sms', data })
}

// C端激活第四步-通过学号查找完整信息
export const getAccountInfo = (data: AppAuthSmsInfoVO) => {
  return request.post({ url: '/system/auth/verify-info', data })
}

// C端激活第五步-激活账号并设置初始密码
export const setPassword = (data: AppAuthActiveReqVO) => {
  return request.put({ url: '/system/auth/set-password', data })
}

// 发送重置联培账号密码短信
export const sendResetPasswordSmsToImrAccount = (data: AuthResetPasswordReqVO) => {
  return request.post({ url: '/system/auth/send-reset-sms', data })
}
// 重置联培账号密码
export const resetPassword = (data: AuthResetPasswordSaveReqVO) => {
  return request.post({ url: '/system/auth/reset-pwd', data })
}

// C端科大统一登录
export const ustcLocalLogin = (data: UserLoginVO) => {
  return request.post({ url: '/system/auth/ustc-local-login', data })
}

// 发送修改科大USTC账号本地密码短信(需要登录)
export const sendResetLocalPasswordSmsToUstcAccount = (
  data: Omit<AuthResetPasswordReqVO, 'username'>
) => {
  return request.post({ url: '/system/auth/send-reset-local-sms', data })
}
// 重置科大USTC账号本地密码
export const resetUstcLocalPassword = (data: Omit<AuthResetPasswordSaveReqVO, 'username'>) => {
  return request.post({ url: '/system/auth/reset-local-pwd', data })
}

// 以下无用但未为删除

// 刷新访问令牌
export const refreshToken = () => {
  return request.post({ url: '/system/auth/refresh-token?refreshToken=' + getRefreshToken() })
}

// 使用租户名，获得租户编号
export const getTenantIdByName = (name: string) => {
  return request.get({ url: '/system/tenant/get-id-by-name?name=' + name })
}

// 使用租户域名，获得租户信息
export const getTenantByWebsite = (website: string) => {
  return request.get({ url: '/system/tenant/get-by-website?website=' + website })
}
